new Vue({
    el: '#app',
    data: {
        loading: false,
        ruleForm: {
            id:'',
            mappid: '',
            mappsecret: '',
        },
    },
    created() {
        this.getInfo()
    },
    methods: {
        reload() {
            location.replace(location.href)
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.update()
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        getInfo() {
            this.loading = true
            let params = {}
            $.ajax({
                type: 'post',
                url: '/wechat/config',
                data: params,
                dataType: 'json',
                success: (res, s, xhr) => {
                    if (res.code == 0) {
                        if(res.data){
                            this.ruleForm.id = res.data.id
                            this.ruleForm.mappid = res.data.mappid
                            this.ruleForm.mappsecret = res.data.mappsecret
                        }

                    } else {
                        this.$message.error(res.msg || '网络异常')
                    }

                    this.loading = false
                },
                error: (xhr, s, err) => {
                    console.log(xhr, s, err)
                    this.$message.error(err.msg || '网络异常')
                    this.loading = false
                    return false;
                }
            });
        },
        // 修改
        update() {
            this.loading = true
            let params = {
                id:this.ruleForm.id,
                mappid: this.ruleForm.mappid,
                mappsecret: this.ruleForm.mappsecret,
            }
            $.ajax({
                type: 'post',
                url: '/wechat/update',
                data: params,
                dataType: 'json',
                success: (res, s, xhr) => {
                    if (res.code == 0) {
                        this.$message.success('修改成功！')
                    } else {
                        this.$message.error(res.msg || '网络异常')
                    }

                    this.loading = false
                },
                error: (xhr, s, err) => {
                    console.log(xhr, s, err)
                    this.$message.error(err.msg || '网络异常')
                    this.loading = false
                    return false;
                }
            });
        },
    }
})
